﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace Jy123.Web.Module.Venues
{
    public class GetState : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string strType = context.Request["Type"];
            string strMId = context.Request["ModuleId"];
            if (!string.IsNullOrEmpty(strMId))
            {
                int mId = int.Parse(strMId);
                BLL.Module_Main BLLModule = new Jy123.BLL.Module_Main();
                BLL.User_Main BLLUser = new Jy123.BLL.User_Main();
                BLL.Public.State BLLState = new Jy123.BLL.Public.State();
                Model.Module_Main module = BLLModule.GetModel(mId);
                int stateType = 0;
                int venuesId = 0;
                string strResult = string.Empty;
                if (strType.Equals("Race"))
                {
                    stateType = Convert.ToInt32(BLL.Public.EnumConfig.StateActionType.Race);
                    venuesId = mId;
                    List<BLL.Public.State.StateClass> listState = BLLState.GetState(Convert.ToInt32(BLL.Public.EnumConfig.StateType.Module), 10, module.FUser.Value, true, false, stateType, venuesId);
                    strResult = GetResultString(listState);
                }
                else {
                    List<BLL.Public.State.StateClass> listAllState = BLLState.GetState(Convert.ToInt32(BLL.Public.EnumConfig.StateType.Module), 10, module.FUser.Value, false, false, stateType, venuesId);
                    strResult = GetResultString(listAllState);
                }               
                
                context.Response.Write(strResult);
            }
        }

        private string GetResultString(List<BLL.Public.State.StateClass> listState)
        {
            StringBuilder sbString = new StringBuilder();
            sbString.Append("<ul>");
            string strCache = "<li><span>$Content$</span></li>";
            string strSpace = string.Empty;
            for (int i = 0; i < 13; i++)
            {
                strSpace += "&nbsp;";
            }
            foreach (BLL.Public.State.StateClass state in listState)
            {
                string strResult = strCache;
                strResult = strResult.Replace("$Content$", state.HTML + strSpace + state.STRTIME);
                sbString.Append(strResult);
            }
            sbString.Append("</ul>");
            return sbString.ToString();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
